<?php 
namespace Admin\Controller;
use Admin\Controller\BaseController;
/**
 * 新闻中心控制器
 * @author 佛祖<381361943@qq.com>
 */
class NewController extends BaseController {
	/**
	 * 新闻列表
	 * @author 佛祖<381361943@qq.com>
	 */
	public function index()
	{
		$new = M('new'); // 实例化User对象
		// 进行分页数据查询 注意page方法的参数的前面部分是当前的页数使用 $_GET[p]获取
		$list = $new->order('id desc')->page($_GET['p'].',10')->select();
		$this->assign('list',$list);// 赋值数据集
		$count      = $new->count();
		$Page       = new \Think\Page($count,10);// 实例化分页类 传入总记录数和每页显示的记录数
		$Page->setConfig('header', '<li class="rows">共<b>%TOTAL_ROW%</b>条记录&nbsp;第<b>%NOW_PAGE%</b>页/共<b>%TOTAL_PAGE%</b>页</li>');
	    $Page->setConfig('prev', '上一页');
	    $Page->setConfig('next', '下一页');
	    $Page->setConfig('last', '末页');
	    $Page->setConfig('first', '首页');
	    $Page->setConfig('theme', '%FIRST%%UP_PAGE%%LINK_PAGE%%DOWN_PAGE%%END%%HEADER%');
	    $Page->lastSuffix = false;//最后一页不显示为总页数
		$show       = $Page->show();// 分页显示输出
		$this->assign('page',$show);// 赋值分页输出
		$this->assign('count',$count );
		$this->display();
	}
	/**
	 * 新闻新增
	 * @author 佛祖<381361943@qq.com>
	 */
	public function add()
	{
		if(IS_GET)
		{
			$this->display();
		}
		else
		{
			$data = I('post.');
			unset($data['__hash__']);
            $count=strpos($data['picture'],get_domain($data['picture']));
            $data['picture']=substr_replace($data['picture'],"",$count,strlen(get_domain($data['picture'])));
            $data['time'] = time();
			M('new')->add($data);
			$this->success('添加成功',U('index'));
		}	
	}
	/**
	 * 新闻修改
	 * @author 佛祖<381361943@qq.com>
	 */
	public function save()
	{
		if(IS_GET)
		{
			$id = I('id');
			$id == ''?$this->error('参数错误'):1;
			$list = M('new')->find($id);
			$list == ''?$this->error('数据不存在'):"1";
			$this->assign('list',$list);
			$this->display();
		}
		else
		{
			$data = I('post.');
			$id = $data['id'];
			unset($data['id']);
			unset($data['__hash__']);
            $count=strpos($data['picture'],get_domain($data['picture']));
            $data['picture']=substr_replace($data['picture'],"",$count,strlen(get_domain($data['picture'])));
			M('new')->where(array('id'=>$id))->save($data);
			//echo M()->_sql();die;
			$this->success('修改成功',U('index'));
		}	
	}
	/**
	 * 新闻删除
	 * @author 佛祖<381361943@qq.com>
	 */
	public function delete()
	{
		$id = I('id');
		$id == ''?$this->error('错误的参数'):1;
		M('new')->delete($id);
		$this->success('删除成功');
	}
}